package com.aliyun.oss.service.impl;

import com.aliyun.oss.mapper.UserMapper;
import com.aliyun.oss.pojo.User;
import com.aliyun.oss.pojo.UserInfo;
import com.aliyun.oss.service.UserService;
import com.aliyun.oss.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
    @Override
    public void add(User user) {
        userMapper.add(user);
    }

    @Override
    public UserInfo userLogin(User user) {
        UserInfo userInfo = userMapper.userLogin(user);
        if(userInfo!=null){
            Map<String,Object> claims=new HashMap<>();
            claims.put("id",userInfo.getId());
            claims.put("username",userInfo.getUsername());
            String jwt= JwtUtil.createToken(claims);
            userInfo.setToken(jwt);
            return userInfo;
        }
        return null;
    }
}
